﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace Jage.Common
{
    public abstract class SavableObject<T>
        where T : class, new()
    {

        static public T LoadFromFile(string filename)
        {
            using (Stream stream = File.OpenRead(filename))
            {
                return LoadFromStream(stream);
            }
        }

        static public T LoadFromStream(Stream stream)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(stream);
        }

        static public void SaveToFile(string filename, T source)
        {
            using (Stream stream = File.Create(filename))
            {
                SaveToStream(stream, source);
            }
        }

        static public void SaveToStream(Stream stream, T source)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(stream, source);
        }

    }
}
